import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get_instance/src/extension_instance.dart';
import 'package:get/state_manager.dart';
import 'package:surfaceim/pages/community/controller.dart';

class CommentsView extends StatelessWidget {
  CommunityController controller = Get.find<CommunityController>();

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 1106.h,
      padding: EdgeInsets.only(top: 35.h, left: 43.w, right: 43.w),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                "全部评论",
                style: TextStyle(
                    color: Color.fromRGBO(51, 51, 51, 1),
                    fontSize: 28.sp,
                    fontWeight: FontWeight.bold),
              ),
              Text(
                "按热门",
                style: TextStyle(
                    color: Color.fromRGBO(102, 102, 102, 1), fontSize: 26.sp),
              )
            ],
          ),
          Expanded(child: Obx(() {
            return ListView.separated(
              itemCount: controller.commentList.length,
              itemBuilder: (context, index) {
                return Container(
                    margin: EdgeInsets.only(top: 24.h),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Row(
                              children: [
                                Container(
                                  width: 60.w,
                                  height: 60.w,
                                  decoration: BoxDecoration(
                                      borderRadius: BorderRadius.circular(30.w),
                                      image: DecorationImage(
                                          fit: BoxFit.cover,
                                          image: AssetImage(
                                              "assets/images/meinv.jpeg"))),
                                ),
                                Container(
                                    margin: EdgeInsets.only(left: 8.w),
                                    child: Text(
                                      controller.commentList[index].name,
                                      style: TextStyle(
                                          color: Color.fromRGBO(51, 51, 51, 1),
                                          fontWeight: FontWeight.bold,
                                          fontSize: 28.sp),
                                    )),
                                Container(
                                    margin: EdgeInsets.only(left: 16.w),
                                    child: Text(
                                      controller.commentList[index].introduce,
                                      style: TextStyle(
                                          color:
                                              Color.fromRGBO(193, 193, 193, 1),
                                          fontSize: 22.sp),
                                    ))
                              ],
                            ),
                            Text("占位")
                          ],
                        ),
                        Container(
                          margin: EdgeInsets.only(top: 19.h),
                          child: Text(
                            controller.commentList[index].content,
                            style: TextStyle(
                                color: Color.fromRGBO(51, 51, 51, 1),
                                fontSize: 28.sp,
                                fontWeight: FontWeight.w400),
                          ),
                        )
                      ],
                    ));
              },
              separatorBuilder: (BuildContext context, int index) {
                Container con = Container();
                if (controller.commentList[index].isComment) {
                  // 不展开
                  List<Widget> list = List.empty(growable: true);
                  for (var i = 0;
                      i < controller.commentList[index].comList.length;
                      i++) {
                    if (i == 2 && !controller.commentList[index].unflod) {
                      list.add(Container(
                          child: InkWell(
                        child: Text(
                          "展开全部",
                          style: TextStyle(
                              color: Color.fromRGBO(41, 135, 255, 1),
                              fontSize: 28.sp),
                        ),
                        onTap: () {
                          List<CommentItem> temp =
                              List.empty(growable: true); //需要新建一个对象，否则无法更改
                          for (int i = 0;
                              i < controller.commentList.length;
                              i++) {
                            if (index == i) {
                              controller.commentList[i].unflod =
                                  !controller.commentList[i].unflod;
                            }
                            temp.add(controller.commentList.value[i]);
                          }
                          controller.commentList(temp);
                        },
                      )));
                      break;
                    }
                    list.add(Container(
                      margin: EdgeInsets.only(top: i == 0 ? 0 : 3.h),
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text(
                            controller.commentList[index].comList[i]["name"]
                                .toString(),
                            style: TextStyle(
                                color: Color.fromRGBO(41, 135, 255, 1),
                                fontSize: 28.sp),
                          ),
                          Text("："),
                          Expanded(
                              child: Text(
                            controller.commentList[index].comList[i]["value"]
                                .toString(),
                            style: TextStyle(
                                color: Color.fromRGBO(51, 51, 51, 1),
                                fontSize: 28.sp),
                          ))
                        ],
                      ),
                    ));
                    // 收起全部
                    if (controller.commentList[index].unflod &&
                        i == controller.commentList[index].comList.length - 1) {
                      list.add(Container(
                          child: InkWell(
                        child: Text(
                          "收起",
                          style: TextStyle(
                              color: Color.fromRGBO(41, 135, 255, 1),
                              fontSize: 28.sp),
                        ),
                        onTap: () {
                          List<CommentItem> temp =
                              List.empty(growable: true); //需要新建一个对象，否则无法更改
                          for (int i = 0;
                              i < controller.commentList.length;
                              i++) {
                            if (index == i) {
                              controller.commentList[i].unflod =
                                  !controller.commentList[i].unflod;
                            }
                            temp.add(controller.commentList.value[i]);
                          }
                          controller.commentList(temp);
                        },
                      )));
                    }
                  }
                  con = Container(
                    margin: EdgeInsets.only(top: 19.h),
                    child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: list),
                  );
                }
                return Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Container(
                      color: Color.fromRGBO(238, 238, 238, 1),
                      height: 1.h,
                      margin: EdgeInsets.only(top: 18.h),
                    ),
                    con
                  ],
                );
              },
            );
          }))
        ],
      ),
    );
  }
}
